package com.ahcloud.edas.rocketmq.core.infrastructure.rocketmq.service.impl;

import com.ahcloud.edas.common.util.JsonUtils;
import com.ahcloud.edas.rocketmq.core.infrastructure.rocketmq.service.AbstractCommonService;
import com.ahcloud.edas.rocketmq.core.infrastructure.rocketmq.service.ClusterService;
import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.remoting.protocol.body.ClusterInfo;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * @program: edas
 * @description:
 * @author: YuKai Fan
 * @create: 2024/5/16 11:26
 **/
@Slf4j
@Service
public class ClusterServiceImpl extends AbstractCommonService implements ClusterService {

    @Override
    public Map<String, Set<String>> clusterBrokerList() {
        try {
            ClusterInfo clusterInfo = mqAdminExt.examineBrokerClusterInfo();
            log.info("op=look_clusterInfo {}", JsonUtils.toJsonString(clusterInfo));
            if (Objects.isNull(clusterInfo)) {
                return Maps.newHashMap();
            }
            return clusterInfo.getClusterAddrTable();
        } catch (Exception e) {
            Throwables.throwIfUnchecked(e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public Set<String> getClusterNameSet() {
        return clusterBrokerList().keySet();
    }
}
